package org.example.myGetSet.service;

import cn.hutool.core.lang.Dict;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.example.myGetSet.utils.WeBASEUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DemoService {
    @Autowired
    WeBASEUtils weBASEUtils;

    @Value("${userAddress}")
    String userAddress;

    @Value("${system.contract.myGetSetAddress}")
    String contractAddress;

    public static final String ABI = org.example.myGetSet.utils.IOUtil.readResourceAsString("abi/MyGetSet.abi");


    public String setService(String input) {
        List funcParam = new ArrayList();
        funcParam.add(input);
        Dict result =weBASEUtils.commonReq(userAddress, "set", funcParam, ABI, "MyGetSet", contractAddress);
        JSONObject json = JSONUtil.parseObj(result.get("result"));
        if (json.get("message").equals("Success")) {
            return "ok";
        }else {
            return "failed";
        }
    }

    public String getService() {
        List funcParam = new ArrayList();
        Dict result =weBASEUtils.commonReq(userAddress, "get", funcParam, ABI, "MyGetSet", contractAddress);
        return (String) result.get("result");
    }
}
